#!/bin/bash

# 核心配置
export PATH="$PATH:/usr/share/maven/bin"
export MAVEN_OPTS="-Dmaven.repo.local=/var/jenkins_home/maven/repository"
export BUILD_MODULE="${BUILD_MODULE}"
export BRANCH="${BRANCH}"

# 输出调试信息
echo "DEBUG: 模块=${BUILD_MODULE}, 分支=${BRANCH}"
echo "PATH: $PATH"
which mvn || echo "mvn未找到"
mvn help:effective-settings | grep -E "localRepository"

# 通用构建函数
build_module() {
    local module_path=$1
    local parent_dir=${2:-}

    if [ -n "$parent_dir" ]; then
        cd "$parent_dir" && mvn install -N
    fi

    cd "$module_path" && mvn clean install
}

# 模块构建逻辑
case "$BUILD_MODULE" in
    "exam-pro-base"|"exam-pro-gateway"|"exam-pro-parent")
        build_module "$BUILD_MODULE"
        ;;
    "exam-pro-course-api")
        build_module "exam-pro-course-api" "exam-pro-course"
        ;;
    "exam-pro-exam-api")
        build_module "exam-pro-exam-api" "exam-pro-exam"
        ;;
    "exam-pro-paper-api")
        build_module "exam-pro-paper-api" "exam-pro-paper"
        ;;
    "exam-pro-question-api")
        build_module "exam-pro-question-api" "exam-pro-question"
        ;;
    "exam-pro-user-api")
        build_module "exam-pro-user-api" "exam-pro-user"
        ;;
    *)
        echo "错误：未知模块 $BUILD_MODULE"
        exit 1
        ;;
esac